﻿// RS232Dlg.cpp: 实现文件
//

#include "pch.h"
#include "ProjectAA.h"
#include "RS232Dlg.h"
#include "afxdialogex.h"


// CRS232Dlg 对话框

IMPLEMENT_DYNAMIC(CRS232Dlg, CDialogEx)

CRS232Dlg::CRS232Dlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_RS232, pParent)
	, m_bPassRfid(FALSE)
	, m_nCom1(0)
	, m_nPort1(0)
	, m_nCom2(0)
	, m_nPort2(0)
{

}

CRS232Dlg::~CRS232Dlg()
{
}

void CRS232Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Check(pDX, IDC_CHECK1, m_bPassRfid);
	DDX_Text(pDX, IDC_EDIT_COM1, m_nCom1);
	DDX_Text(pDX, IDC_EDIT_PORT1, m_nPort1);
	DDX_Text(pDX, IDC_EDIT_COM2, m_nCom2);
	DDX_Text(pDX, IDC_EDIT_PORT3, m_nPort2);
}


BEGIN_MESSAGE_MAP(CRS232Dlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &CRS232Dlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CRS232Dlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON6, &CRS232Dlg::OnBnClickedButton6)
END_MESSAGE_MAP()


// CRS232Dlg 消息处理程序


BOOL CRS232Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	ReadConfigIni();

	return TRUE;
}


void CRS232Dlg::OnCancel()
{
	// TODO: 在此添加专用代码和/或调用基类

	CDialogEx::OnCancel();
}

void CRS232Dlg::ReadConfigIni()
{
	m_nCom1 = gSystemMgr.m_nComNo1;
	m_nPort1 = gSystemMgr.m_nComPort1;
	m_nCom2 = gSystemMgr.m_nComNo2;
	m_nPort2 = gSystemMgr.m_nComPort2;
	m_bPassRfid = gSystemMgr.m_nPassRfidFlag;
	UpdateData(FALSE);
}

void CRS232Dlg::SaveValueToConfig()
{
	UpdateData(TRUE);

	gSystemMgr.m_nComNo1 = m_nCom1;
	gSystemMgr.m_nComPort1 = m_nPort1;
	gSystemMgr.m_nComNo2 = m_nCom2;
	gSystemMgr.m_nComPort2 = m_nPort2;
	gSystemMgr.m_nPassRfidFlag = m_bPassRfid;

	CString cstr;
	cstr.Format(L"%d", gSystemMgr.m_nComNo1);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComNo1", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nComPort1);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComPort1", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nComNo2);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComNo2", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nComPort2);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nComPort2", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nPassRfidFlag);
	gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"RS232", L"m_nPassRfidFlag", cstr);

	gSystemLog.DisplayLogAndSave(L"RS232保存数据完成", LV_EVENT);
}





void CRS232Dlg::OnBnClickedButton1()
{
	SaveValueToConfig();
}


void CRS232Dlg::OnBnClickedButton2()
{
	if (gSystemMgr.m_cRfidComm1.get_com_port() == NULL) {
		int err = gSystemMgr.m_cRfidComm1.com_open(gSystemMgr.m_nComNo1, gSystemMgr.m_nComPort1, EVENPARITY);
		if (err != 1) {
			CString cstr;
			cstr.Format(L"Com1 Error %d", err);
			gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
			gSystemMgr.Com1ConnectFlag = 0;
		}
		else
			gSystemMgr.Com1ConnectFlag = 1;
	}
}


void CRS232Dlg::OnBnClickedButton6()
{
	if (gSystemMgr.m_cRfidComm2.get_com_port() == NULL) 
	{
		int err = gSystemMgr.m_cRfidComm2.com_open(gSystemMgr.m_nComNo2, gSystemMgr.m_nComPort2, EVENPARITY);
		if (err != 1) 
		{
			CString cstr;
			cstr.Format(L"Com2 Error %d", err);
			gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
			gSystemMgr.Com2ConnectFlag = 0;
		}
		else
			gSystemMgr.Com2ConnectFlag = 1;
	}
}